package com.cxy.service.Impl;

import com.cxy.Exception.ImportUpdateException;
import com.cxy.dto.InformationDTO;
import com.cxy.dto.InformationImportDTO;
import com.cxy.dto.InformationScoreDTO;
import com.cxy.entity.Information;
import com.cxy.entity.Task;
import com.cxy.mapper.InformationMapper;
import com.cxy.mapper.TaskMapper;
import com.cxy.mapper.UserMapper;
import com.cxy.result.PageResult;
import com.cxy.service.InformationService;
import com.cxy.utils.ExcelUtil;
import com.cxy.vo.InformationUpdateVO;
import com.cxy.vo.InformationVO;
import com.cxy.vo.UserVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TaskMapper taskMapper;
    /**
     * 查询评议信息管理
     * @param informationDTO
     * @return
     */
    public PageResult page(InformationDTO informationDTO) {
        PageHelper.startPage(informationDTO.getPage(),informationDTO.getPageSize());
        Page<InformationVO>page=informationMapper.page(informationDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询详细评议信息
     * @param id
     * @return
     */
    public InformationUpdateVO getById(Long id) {
        Information information = informationMapper.getById(id);
        InformationUpdateVO informationVO =new InformationUpdateVO();
        BeanUtils.copyProperties(information,informationVO);
        return informationVO;
    }

    /**
     * 修改评分
     * @param informationScoreDTO
     */
    public void update(InformationScoreDTO informationScoreDTO) {
        Double allScore = informationScoreDTO.getDemocraticScore()+ informationScoreDTO.getBranchScore()+ informationScoreDTO.getSecretaryScore();
        informationScoreDTO.setGrossScore(allScore);
        if (informationScoreDTO.getDemocraticScore()!=0&&informationScoreDTO.getBranchScore()!=0&&informationScoreDTO.getSecretaryScore()!=0){
            informationScoreDTO.setStatus("2");
        }
        informationMapper.update(informationScoreDTO);
    }

    /**
     * 导出模板
     * @param response
     * @param request
     */
    public void export(HttpServletResponse response, HttpServletRequest request) {
        try {
            ExcelUtil.solveRequestHead("评议信息数据模板.xlsx",response,request);
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/评议信息数据模板.xlsx");
            XSSFWorkbook excel =new XSSFWorkbook(in);
            //通过输出流excel文件下载到客户端浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            excel.write(outputStream);

            outputStream.close();
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入评议信息数据文件
     */
    public void dataImport(MultipartFile file)  throws IOException{
        InformationImportDTO informationImportDTO =new InformationImportDTO();
       // Task task = new Task();
        //用workbook读取传递过来的文件的信息
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取sheet
        Sheet sheet =workbook.getSheetAt(0);
        //获得最后一行
        int lastRowNum = sheet.getLastRowNum();
        //进行遍历
        for(int i = 1; i < lastRowNum; i++) {
            //获取行
            Row row = sheet.getRow(i);
            //获取单元格
            String name = row.getCell(0).getStringCellValue();
            String dateNumber = row.getCell(1).getStringCellValue();
            double democraticScore = row.getCell(2).getNumericCellValue();
            double branchScore = row.getCell(3).getNumericCellValue();
            double secretaryScore = row.getCell(4).getNumericCellValue();
            List<UserVO> users = userMapper.select();
            for (UserVO user : users) {
               if (user.getName().equals(name)){
                   List<Task> tasks = taskMapper.selectAll();
                   for (Task task : tasks) {
                       if (task.getDateNumber().equals(dateNumber)){
                           informationImportDTO.setGrossScore(democraticScore+branchScore+secretaryScore);
                           informationImportDTO.setUserId(user.getId());
                           informationImportDTO.setTaskId(task.getId());
                           informationImportDTO.setDemocraticScore(democraticScore);
                           informationImportDTO.setBranchScore(branchScore);
                           informationImportDTO.setSecretaryScore(secretaryScore);
                           if (democraticScore!=0&&branchScore!=0&&secretaryScore!=0){
                               informationImportDTO.setStatus("2");
                           }
                           informationMapper.importUpdate(informationImportDTO);
                       }else {
                           throw new ImportUpdateException("您传递的期数错误");
                       }
                   }
               }else {
                   throw new ImportUpdateException("您传递的名字错误");
               }
            }
        }
    }

    /**
     * 导入数据成绩修改
     */
    public void ImportUpdate(InformationImportDTO informationImportDTO) {
        informationMapper.importUpdate(informationImportDTO);
    }


}
